//2023 02.13 17:07 星期一 雨
//--2023 02.18 20:13 星期六 寫完 陰
//-- 2023 11.30 16：54 星期四 改写 阴
/**
 * 提取 json to string
 * 提取 style Element to from_string
 */

// import { StackStyle } from '../styleStr2Json';
import { defaultSequence } from './styleSequence';
import { settleWeightArr } from '../../../digitalFunc/settleWeightArr';

/**
 * analysis  style 的 排序方式
 */
let codeSequen;

/**
 * 格式化 链接 style json 综合 string
 */
let strjoin;

/**
 * 对 style stackJson 进行排序 和 阶层 整合为
 * @param {Object} stackJson style Json Data -- stackStyle 解析和的 json 数据
 * @param {Object} userSequence 用户 自定义 序列
 */
function analysisStyleStack(stackJson, userSequence) {
  //   let stackJson = StackStyle(styleEl);

  //是否需要比对??
  if (userSequence != undefined) {
    codeSequen = userSequence;
  } else {
    codeSequen = defaultSequence;
  }

  //权重赋值
  weightStackJson(stackJson);

  //权重解析
  releaseWeight(stackJson);

  //整合 style json 数据
  //   stackToString(stackJson, 0);
  //   console.log(stackJson);
  //   return stackJson;
  return stackToString(stackJson);
}

/**
 * 定位权重
 * @param {*} str css 单个属性
 * @returns [总体位置,单个位置]
 */
function locatePosition(str) {
  for (let i = 0; i < codeSequen.length; i++) {
    if (codeSequen[i].indexOf(str) > -1) {
      let stairs = i;
      let index = codeSequen[i].indexOf(str);
      // console.log(str, stairs, index);
      return [stairs, index];
    }
  }
}

/**
 * 对 新解析 后的数据 经行 权重赋值
 * @param {JSON} stack StackStyle 经过 stylestack 解析后的 json 数据
 */
function weightStackJson(stack) {
  if (!stack['$digital']) {
    for (let j = 0; j < stack['$data'].length; j++) {
      weightStackJson(stack[stack['$data'][j]]);
    }
  } else {
    stack['$weight'] == undefined ? (stack['$weight'] = {}) : undefined;
    for (let i = 0; i < stack['$data'].length; i++) {
      stack['$weight'][stack['$data'][i]] = locatePosition(stack['$data'][i]);
    }
  }
}

/**
 * 解析 权重 数据
 * @param {JSON} stack 经过 权重赋值 的 json 数据
 */
function releaseWeight(stack) {
  if (!stack['$digital']) {
    for (let j = 0; j < stack['$data'].length; j++) {
      releaseWeight(stack[stack['$data'][j]]);
    }
  } else {
    stack['$releaseW'] == undefined ? (stack['$releaseW'] = {}) : undefined;
    for (let i = 0; i < stack['$data'].length; i++) {
      stack['$releaseW']['$data'] == undefined
        ? (stack['$releaseW']['$data'] = [
            stack['$weight'][stack['$data'][i]][0],
          ])
        : settleWeightArr(
            stack['$releaseW']['$data'],
            stack['$weight'][stack['$data'][i]][0]
          );
      // console.log(stack["$data"][i], stack["$weight"][stack["$data"][i]]);
      stack['$releaseW'][stack['$weight'][stack['$data'][i]][0]] == undefined
        ? (stack['$releaseW'][stack['$weight'][stack['$data'][i]][0]] = [
            stack['$weight'][stack['$data'][i]][1],
          ])
        : settleWeightArr(
            stack['$releaseW'][stack['$weight'][stack['$data'][i]][0]],
            stack['$weight'][stack['$data'][i]][1]
          );
    }
  }
}

/**
 * 根據權重 推演 排序 輸出
 * @param {JSON} stack style string 解析后 并 进行过 权重赋值 和 权重解析 的 json 数据
 * @param {Number} deep 数据深度 缩进 (table) 格式的 深度
 * @returns
 */
function stackToString(stack, deep) {
  deep == undefined ? (deep = 0) : undefined;
  strjoin = '';
  if (!stack['$digital']) {
    for (let i = 0; i < stack['$data'].length; i++) {
      formPrefix(deep);
      strjoin += stack['$data'][i];

      strjoin += ' ';

      strjoin += '{';
      strjoin += '\n';

      strjoin += stackToString(stack[stack['$data'][i]], deep + 1);

      formPrefix(deep);

      strjoin += '}';
      strjoin += '\n';
    }
  } else {
    let seqedProps = [];
    for (let m = 0; m < stack['$releaseW']['$data'].length; m++) {
      m == 0 ? undefined : (strjoin += '\n');
      let x = stack['$releaseW']['$data'][m];

      for (let n = 0; n < stack['$releaseW'][x].length; n++) {
        let targetKey = defaultSequence[x][stack['$releaseW'][x][n]];
        if (stack['$data'].indexOf(targetKey) > -1) {
          seqedProps.push(targetKey);
          //核心
          formPrefix(deep);
          strjoin += targetKey;
          strjoin += ':';
          strjoin += ' ';

          strjoin += stack[targetKey];

          strjoin += ';\n';
          //核心
        }
      }
    }
    for (var i = 0; i < stack['$data'].length; i++) {
      if (seqedProps.indexOf(stack['$data'][i]) == -1) {
        //核心
        formPrefix(deep);
        strjoin += stack['$data'][i];
        strjoin += ':';
        strjoin += ' ';

        strjoin += stack[stack['$data'][i]];

        strjoin += ';\n';
        //核心
      }
    }
  }

  return strjoin;
}

/**
 * 格式化 缩进
 * @param {number} deep 格式化 缩进 深度
 */
function formPrefix(deep) {
  for (var i = 0; i < deep; i++) {
    strjoin += '\t';
  }
}

export { analysisStyleStack };
